<template>
  <a-modal
    :title="currentRow ? '编辑' : '新增'"
    :visible="dialogVisible"
    okText="确认"
    cancelText="取消"
    :width="620"
    :confirmLoading="loading"
    @ok="handleSure"
    @cancel="
      () => {
        $emit('cancel');
      }
    "
  >
    <a-form-model
      :model="fileFrom"
      :label-col="{ span: 4 }"
      :wrapper-col="{ span: 16 }"
      ref="userFrom"
      hideRequiredMark
    >
      <a-form-model-item prop="id" label="id" v-show="currentRow">
        <a-input v-model="fileFrom.id" disabled />
      </a-form-model-item>
      <a-form-model-item prop="fileName" label="文件名" hasFeedback>
        <a-textarea v-model="fileFrom.fileName" :autoSize="{ minRows: 3, maxRows: 5 }" />
      </a-form-model-item>
      <a-form-model-item prop="fileType" label="文件类型" hasFeedback>
        <a-input v-model="fileFrom.fileType" />
      </a-form-model-item>
      <a-form-model-item prop="filePath" label="文件路径" hasFeedback>
        <a-input v-model="fileFrom.filePath" />
      </a-form-model-item>
      <a-form-model-item prop="md5" label="MD5" hasFeedback>
        <a-input v-model="fileFrom.md5" disabled />
      </a-form-model-item>
      <a-form-model-item prop="fileStatus" label="文件状态">
        <a-radio-group v-model="fileFrom.fileStatus">
          <a-radio v-for="item in roleOption" :key="item.key" :value="item.key">
            {{ item.label }}
          </a-radio>
        </a-radio-group>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
import { editTable, addTable } from '@/api/fileManage';
import standardTree from '@/components/standardTree/index';
export default {
  name: 'fileModel',
  props: {
    currentRow: [Object, null],
    dialogVisible: Boolean,
    roleOption: Array
  },
  components: { standardTree },
  data() {
    return {
      fileFrom: {
        role: ''
      },
      loading: false
    };
  },

  mounted() {
    this.fileFrom = { ...this.currentRow } || {
      role: ''
    };
  },
  methods: {
    handleSure() {
      this.loading = true;
      const { id, fileName, filePath, directoryId, md5, fileType, userId, fileStatus, fileSize } = this.fileFrom;
      if (this.currentRow) {
        //编辑
        editTable({ id, fileName, filePath, directoryId, md5, fileType, userId, fileStatus, fileSize }).then(() => {
          this.$message.success('修改成功!');
          this.loading = false;
          this.$emit('ok');
        });
      } else {
        //新增
        addTable({ id, fileName, filePath, directoryId, md5, fileType, userId, fileStatus, fileSize }).then(() => {
          this.$message.success('添加成功!');
          this.loading = false;
          this.$emit('ok');
        });
      }
      this.loading = false;
    }
  }
};
</script>
